package com.duanjw.springcloud.hystrix.web;

import com.duanjw.springcloud.hystrix.feign.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author duanjw
 */
@RestController
public class WebController {
    private final OrderService orderService;

    public WebController(OrderService orderService) {
        this.orderService = orderService;
    }
    /**
     * 使用feign调用订单接口
     *
     * 调用地址：curl localhost:7002/order-feign
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    @GetMapping("order-feign")
    public String orderByFeign(){
        System.out.println(1/0);
        String orderResult = orderService.order();
        return orderResult;
    }

    /**
     * 熔断测试
     *
     * 调用地址：curl localhost:7001/order-error
     * @return
     */
    @GetMapping("hystrix-error")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    @ResponseBody
    public String error(){
        System.out.println("进入hystrix-error接口");
        System.out.println(1/0);
        return "error";
    }

    public String fallbackMethod(){

        System.out.println("hystrix服务挂了");
        return "hystrix服务挂了";
    }
}
